package com.ztesoft.ossup.report.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztesoft.ossup.report.dao.JdbcTemplateDao;
import com.ztesoft.ossup.report.dao.ReportColumnGroupRepo;
import com.ztesoft.ossup.report.dao.ReportColumnRepo;
import com.ztesoft.ossup.report.dao.ReportConditionEnumRepo;
import com.ztesoft.ossup.report.dao.ReportConditionRepo;
import com.ztesoft.ossup.report.dao.ReportRepository;
import com.ztesoft.ossup.report.dao.ReportSubRepo;
import com.ztesoft.ossup.report.domain.Report;
import com.ztesoft.ossup.report.domain.ReportColumn;
import com.ztesoft.ossup.report.domain.ReportColumnGroup;
import com.ztesoft.ossup.report.domain.ReportCondition;
import com.ztesoft.ossup.report.domain.ReportConditionEnum;
import com.ztesoft.ossup.report.service.BaseService;
import com.ztesoft.ossup.report.service.ColumnConfService;
import com.ztesoft.ossup.report.service.DataPreviewService;
import com.ztesoft.ossup.report.service.PoiService;



/**
 * Created by luxiang on 16/9/20.
 */
@Controller
@RequestMapping("/report")
public class ReportController extends BaseController{
    Logger _logger = Logger.getLogger(ReportController.class);

    @Autowired
    ReportRepository reportRepository;
    
    @Autowired
    ReportConditionEnumRepo reportConditionEnumRepo;
    
    @Autowired
    ReportConditionRepo reportConditionRepo;
    
    @Autowired
    JdbcTemplateDao jdbcTemplateDao;
    
    @Autowired
    ReportColumnRepo reportColumnRepo;
    
    @Autowired
	DataPreviewService dataPreviewService;
    
    @Autowired
    PoiService poiService;
    
    @Autowired
	ReportSubRepo reportSubRepo;
    
    @Autowired
    ColumnConfService columnConfService;
    
    @Autowired
    BaseService baseService;
    
    @Autowired
    ReportColumnGroupRepo groupRepo;

    /**
     * 钻取报表
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String drillReport(@PathVariable String id, Model model) {
    	
       // model.addAttribute("animals", m);
        System.out.println(id);
        return "/report/base";
    }
    
    @RequestMapping(value = "/queryLinkReportInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map queryLinkReportInfo(@RequestBody Map<String, Object> paramMap) {
    	
    	_logger.info("执行queryLinkReportInfo方法(查询钻取报表字段)");
    	long startTime = System.currentTimeMillis();
        int reportId = Integer.valueOf(paramMap.get("reportId").toString());
        String colunmName = (String) paramMap.get("colunmName");
        ReportColumn reportData = reportColumnRepo.findByCodeAndReportId(colunmName, Long.valueOf(reportId));
        String linkReport = reportData.getLinkReport();
        List<Report> drillReportList = reportRepository.findByCode(linkReport);//钻取报表
        if (drillReportList == null || drillReportList.size() == 0) return null;
        Report drillReport = drillReportList.get(0);
        List<ReportColumn> reportColList = reportColumnRepo.findByReportIdOrderBySortOrderAsc(drillReport.getId());
        _logger.info("reportColList:" + reportColList.size());
        List<ReportColumnGroup> reportColumnGroupList = groupRepo.findByReportId(drillReport.getId());
        Map retMap = new HashMap();
        retMap.put("reportColList", reportColList);
        retMap.put("reportId", drillReport.getId());
        retMap.put("reportCode", drillReport.getCode());
        retMap.put("reportTitle", drillReport.getName());
        retMap.put("isSum", drillReport.getIsSum());
        retMap.put("reportColumnGroupList", reportColumnGroupList);
        
        _logger.info("报表编码为："+linkReport +" 的字段数量为："+reportColList.size());
        long endTime = System.currentTimeMillis();
    	_logger.info("加载钻取报表字段耗时："+(endTime - startTime)/1000 +"秒");
        return retMap;
    }
    
    /**	
     * 获取条件数据
     * @param
     * @param model
     * @return
     */
    @RequestMapping(value = "/conditionlist/{code}", method = RequestMethod.GET)
    public String getConditionList(@PathVariable String code,@RequestParam(value = "paramStr",required = false) String paramStr,Model model) {
    	
    	_logger.info("执行conditionlist方法(主报表入口方法)");
    	_logger.info("传入code参数："+code);
    	long startTime = System.currentTimeMillis();
    	try{
    		Report report = null;
        	List<Report> reportList= reportRepository.findByCode(code);//
        	if (reportList != null && reportList.size() > 0){
        		report = reportList.get(0);
        	}
        	_logger.info("传入reportId参数："+report.id);
        	List<ReportCondition> reportConditionList = reportConditionRepo.findByReportIdOrderBySortOrderAsc(report.id);
        	_logger.info("reportCondition参数："+reportConditionList.size());
        	List<ReportConditionEnum> reportConditionEnumlist = new ArrayList<ReportConditionEnum>();
        	List<Map> conditionValList = new ArrayList<Map>();

        	//处理请求过来的参数
			String initParamStr = paramStr == null? "" :paramStr;
			Map requestMap =new HashMap();
			if (!initParamStr.equals("")){
				String[] paramArray = initParamStr.split(",");
				for (int k = 0;k < paramArray.length;k++){
					String[] pArray = paramArray[k].split(":");
					requestMap.put(pArray[0],pArray[1]);
				}
			}

        	for (int i = 0; i < reportConditionList.size(); i++) {
        		ReportCondition repotCondition = reportConditionList.get(i);
        		if (repotCondition.getType().equals("3")){//选择框
        			if (repotCondition.getComboboxType() == 1){//静态数据
            			List<ReportConditionEnum> enumList = reportConditionEnumRepo.selectDataByStype(repotCondition.getEnumCode());
            			_logger.info("enumList参数："+enumList.size());
            			for (int j = 0; j < enumList.size(); j++) {
            				_logger.info("conditionid参数：");
        				}
            			reportConditionEnumlist.addAll(enumList);
            		}else if(repotCondition.getComboboxType() == 2){//sql 
            			if (repotCondition.dataSql != null && !repotCondition.dataSql.equals("")){
            				if (repotCondition.getCascadeCondition() == null || repotCondition.getCascadeCondition().equals("")){
            					String conditionSql = repotCondition.dataSql;
            					if (!initParamStr.equals("")){
										Iterator itera = requestMap.entrySet().iterator();
										while(itera.hasNext()){
											_logger.info("---------处理报表条件Map数据----------");
											Map.Entry entry = (Map.Entry) itera.next();
											String pkey = entry.getKey().toString().toUpperCase();
											String pvalue = entry.getValue()==null?"":entry.getValue().toString();
											_logger.info(pkey+":"+pvalue);
											if (conditionSql.indexOf("#"+pkey+"#") > -1 && !pvalue.equals("")){
												conditionSql = conditionSql.replaceAll("#"+pkey+"#", "'"+pvalue+"'");
											}
										}
								}
            					List<Map<String, Object>> conditionValuesList = jdbcTemplateDao.getConditionValues(conditionSql);
                				if(conditionValuesList != null && conditionValuesList.size() > 0 ){
                					for (Map<String, Object> valuesMap : conditionValuesList) {//在返回的Map中带conditionId
                						valuesMap.put("conditionId", repotCondition.id);
                						conditionValList.add(valuesMap);
            						}
                				}
            				}   				
            			}
            		}
        		}else if(repotCondition.getType().equals("2")){//1 输入框 2 时间框  3选择框

					String conditionDefaultVal = repotCondition.getDefaultVal() == null?"":repotCondition.getDefaultVal();
        			String defaultVal =dataPreviewService.transforDateForReport(conditionDefaultVal);
        			repotCondition.setDefaultVal(defaultVal);
        		}else if(repotCondition.getType().equals("1")){//1 输入框 2 时间框  3选择框
					String conditionVal  = repotCondition.getDefaultVal() == null? "":repotCondition.getDefaultVal();
					if (conditionVal.equals("")){
						repotCondition.setDefaultVal("");
					}else {
						repotCondition.setDefaultVal(conditionVal);
					}
        		}
		}
        	_logger.info("reportConditionEnumlist参数："+reportConditionEnumlist.size());
        	model.addAttribute("enumList", reportConditionEnumlist);
        	model.addAttribute("reportCondition", reportConditionList);
        	model.addAttribute("conditionValList", conditionValList);
        	model.addAttribute("reportId",report.id);
        	model.addAttribute("reportObj",report);
        	model.addAttribute("paramStr",paramStr==null?"":paramStr);
        	
        	long endTime = System.currentTimeMillis();
        	_logger.info("加载主页条件耗时："+(endTime - startTime)/1000 +"秒");
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	    
        return "/report/base";
    }
    
    @RequestMapping(value = "/queryData", method = RequestMethod.POST)
    @ResponseBody
    public com.ztesoft.ossup.report.domain.PageData<Map<String, Object>> queryData(@RequestBody Map<String, Object> paramMap){
		_logger.info("---------执行查询主报表数据方法----------");
		long startTime = System.currentTimeMillis();
		String reportId = paramMap.get("reportId").toString();
		reportId = reportId.replaceAll(",", "");
		int offset = (int) paramMap.get("offset");
		int limit = (int) paramMap.get("limit");

		Report report = reportRepository.findOne(Long.valueOf(reportId));

		String dataSql = baseService.doSql(report.getSql(), paramMap);
		com.ztesoft.ossup.report.domain.PageData<Map<String, Object>> pageData = dataPreviewService.getDataBySql(dataSql, offset,
				limit,0);
		
		long endTime = System.currentTimeMillis();
    	_logger.info("加载主页数据耗时："+(endTime - startTime)/1000 +"秒");
		return pageData;
    }
    
    @RequestMapping(value = "/queryColumnAndData", method = RequestMethod.POST)
    @ResponseBody
    public Map queryColumnAndData(@RequestBody Map<String, Object> paramMap){
    	List<Map<String, Object>> dataList = null;
    	List<ReportColumn> reportColList = null;
    	try{
    		String reportId = (String) paramMap.get("reportId");
        	_logger.info("reportI:"+reportId);
        	reportColList = reportColumnRepo.findByReportIdOrderBySortOrderAsc(Long.valueOf(reportId));     	
        	_logger.info("reportColList:"+reportColList.size());
        	Report report = reportRepository.findOne(Long.valueOf(reportId));
        	
        	String dataSql = report.getSql();
//        	List<ReportQuerySql> sqlList = reportQuerySqlRepo.findByReport_id(Long.valueOf(reportId));
//        	String dataSql = "";
//        	if (sqlList.size() == 1){
//        		dataSql = sqlList.get(0).getQuerySql();
//        	}else if(sqlList.size() > 1){
//        		dataSql = getQuerySqlWithCascade(sqlList,paramMap);
//        	}	
        	if (dataSql != null && !dataSql.equals("")){
        		Iterator itera = paramMap.entrySet().iterator();    	
            	while(itera.hasNext()){
            		//_logger.info("---------Map数据----------");
            		Map.Entry entry = (Map.Entry) itera.next();
            		String key = (String) entry.getKey();
            		String value = (String) entry.getValue();
            		_logger.info(key+":"+value);
            		if (dataSql.indexOf("#"+key+"#") > -1 && !value.equals("") && value != null){
            			dataSql = dataSql.replaceAll("#"+key+"#", "'"+value+"'");
            		}
            	}
            	//dataSql = dataSql.replaceAll("where city_code =#cityName#", "");
            	dataList = jdbcTemplateDao.getConditionValues(dataSql);
        	}else{
        		_logger.error(getI18NMessage("msg.reportSqlIsNull"));
        	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
//    	List<ReportColumn> removeReportColList = new ArrayList<ReportColumn>();
//    	for (ReportColumn reportData : reportColList) {
//    		String colCode = reportData.getCode();
//    		int canBeNone = reportData.getCanBeNone();
//    		if (canBeNone == 1 ){
//    			String flag ="0";
//    			for (Map<String, Object> dataMap : dataList) {
//					String colVal = dataMap.get(colCode) == null?"":dataMap.get(colCode).toString();
//					if (!colVal.equals("-2")){//剔除字段数据都为－2的数据    这部分字段为不显示字段
//						flag ="1";
//					}
//				}
//    			if (flag.equals("0")){
//    				removeReportColList.add(reportData);
//    			}
//    		}
    		
//		}
//    	if (removeReportColList.size() > 0 ){
//    		reportColList.removeAll(removeReportColList);
//    	}
    	Map retMap = new HashMap();
    	retMap.put("reportColList", reportColList);
    	retMap.put("reportDataList", dataList);
    	return retMap;
    }
    
    @RequestMapping(value = "/queryColumn", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> queryColumn(@RequestBody Map<String, Object> paramMap){
    	
    	_logger.info("执行queryColumn方法(查询主报表字段)");
    	long startTime = System.currentTimeMillis();
    	List<ReportColumn> reportColList = null;
    	Report report = null;
    	List<ReportColumnGroup> reportColumnGroupList = null;
    	try{
    		String reportId = (String) paramMap.get("reportId");
    		reportId = reportId.replaceAll(",", "");
        	_logger.info("reportI:"+reportId);
        	reportColList = reportColumnRepo.findByReportIdOrderBySortOrderAsc(Long.valueOf(reportId));
        	report= reportRepository.findOne(Long.parseLong(reportId));
        	reportColumnGroupList = groupRepo.findByReportId(Long.valueOf(reportId));
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	Map retMap = new HashMap();
    	retMap.put("reportColList", reportColList);
    	retMap.put("isSum", report.getIsSum());
    	retMap.put("reportColumnGroupList", reportColumnGroupList);
    	retMap.put("reportTitle", report.getName());
    	
    	long endTime = System.currentTimeMillis();
    	_logger.info("加载主页字段耗时："+(endTime - startTime)/1000 +"秒");
    	return retMap;
    }
    
    @RequestMapping(value = "/queryDrillColumn", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> queryDrillColumn(@RequestBody Map<String, Object> paramMap){
    	List<ReportColumn> reportColList = null;
    	Report drillReport = null;
    	try{
    		String reportId = (String) paramMap.get("reportId");
    		String colunmName = (String) paramMap.get("colunmName");
        	_logger.info("reportI:"+reportId);
        	ReportColumn reportColumn = reportColumnRepo.findByCodeAndReportId(colunmName,Long.valueOf(reportId));
        	String linkReport = reportColumn.getLinkReport();
        	drillReport = reportRepository.findByCode(linkReport).get(0);//钻取报表
        	reportColList = reportColumnRepo.findByReportIdOrderBySortOrderAsc(Long.valueOf(drillReport.getId()));    
        	_logger.info("reportColList:"+reportColList.size());
        	
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	Map retMap = new HashMap();
    	retMap.put("reportColList", reportColList);
    	retMap.put("drillReportTitle", drillReport.getName());
    	retMap.put("isSum", drillReport.getIsSum());
    	return retMap;
    }
    
    @RequestMapping(value = "/queryDrillData", method = RequestMethod.POST)
    @ResponseBody
    public com.ztesoft.ossup.report.domain.PageData queryDrillData(@RequestBody Map<String, Object> paramMap){
    	
    	_logger.info("执行queryDrillData方法(查询钻取报表数据)");
    	long startTime = System.currentTimeMillis();
    	Report drillReport = null;
    	com.ztesoft.ossup.report.domain.PageData pageData = null;
    	try{
    		String reportId = paramMap.get("reportId").toString();
    		int offset = (int) paramMap.get("offset");
    		int limit = (int) paramMap.get("limit");
    		String colunmName = (String) paramMap.get("colunmName");
        	_logger.info("reportId:"+reportId);
//        	ReportColumn reportColumn = reportColumnRepo.findByCodeAndReportId(colunmName,Long.valueOf(reportId));
//        	String linkReport = reportColumn.getLinkReport();
//        	drillReport = reportRepository.findByCode(linkReport).get(0);//钻取报表
        	
        	if (reportId != null && !reportId.equals("")) {
        		drillReport = reportRepository.findOne(Long.parseLong(reportId));
	    	} else {
	            _logger.error("报表查询reportId为空，请详查！！");
	        }
        	String dataSql = drillReport.getSql();
        	
        	if (dataSql != null && !dataSql.equals("")){
        		dataSql = dataPreviewService.doSql(dataSql, paramMap);
        		pageData = dataPreviewService.getDataBySql(dataSql, offset,limit,0);
        	}else{
        		_logger.error(getI18NMessage("msg.reportSqlIsNull"));
        	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	long endTime = System.currentTimeMillis();
    	_logger.info("加载钻去数据耗时："+(endTime - startTime)/1000 +"秒");
    	
		return pageData;
    }
    
    @RequestMapping(value = "/queryDrillReport", method = RequestMethod.POST)
    @ResponseBody
    public Map queryDrillReport(@RequestBody Map<String, Object> paramMap){
    	Report drillReport = null;
    	List<ReportColumn> reportColList = null;
    	List<Map<String, Object>> dataList = null;
    	try{
    		String reportId = (String) paramMap.get("reportId");
        	String colunmName = (String) paramMap.get("colunmName");
        	ReportColumn reportColumn = reportColumnRepo.findByCodeAndReportId(colunmName,Long.valueOf(reportId));
        	String linkReport = reportColumn.getLinkReport();
        	drillReport = reportRepository.findByCode(linkReport).get(0);//钻取报表
        	
        	reportColList = reportColumnRepo.findByReportIdOrderBySortOrderAsc(Long.valueOf(drillReport.getId()));    
        	_logger.info("reportColList:"+reportColList.size());
        	
        	//List<ReportQuerySql> sqlList = reportQuerySqlRepo.findByReport_id(Long.valueOf(drillReport.getId()));
        	String dataSql = drillReport.getSql();
//        	if (sqlList.size() == 1){
//        		dataSql = sqlList.get(0).getQuerySql();
//        	}else if(sqlList.size() > 1){
//        		dataSql = getQuerySqlWithCascade(sqlList,paramMap);
//        	}
        	
        	if (dataSql != null && !dataSql.equals("")){
        		Iterator itera = paramMap.entrySet().iterator();    	
            	while(itera.hasNext()){
            		//_logger.info("---------Map数据----------");
            		Map.Entry entry = (Map.Entry) itera.next();
            		String key = (String) entry.getKey();
            		String value = entry.getValue() == null?"":entry.getValue().toString();
            		_logger.info(key+":"+value);
            		if (dataSql.indexOf("#"+key+"#") > -1 && !value.equals("") && value != null){
            			dataSql = dataSql.replaceAll("#"+key+"#", "'"+value+"'");
            		}
            	}
            	dataList = jdbcTemplateDao.getConditionValues(dataSql);
        	}else{
        		_logger.error(getI18NMessage("msg.reportSqlIsNull"));
        	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
    	Map retMap = new HashMap();
    	retMap.put("reportColList", reportColList);
    	retMap.put("reportDataList", dataList);
    	retMap.put("drillReportTitle", drillReport.getName());
    	return retMap;
    }
    
    @RequestMapping(value = "/queryConditionVal", method = RequestMethod.POST)
    @ResponseBody
    public List<Map<String, Object>> queryConditionVal(@RequestBody Map<String, Object> paramMap){
    	
    	List<Map<String, Object>> dataList = null;
    	try{
	    	String reportId = (String) paramMap.get("reportId");
	    	String paramName = (String) paramMap.get("paramName");
	    	String paramValue = (String) paramMap.get("paramValue"); 
	    	String linkCode = (String) paramMap.get("linkCode"); 
	    	List<ReportCondition> conditionList = reportConditionRepo.findByReportIdAndCode(Long.valueOf(reportId), linkCode);
	    	String dataSql ="";
	    	if(conditionList != null && conditionList.size() > 0){
	    		dataSql = conditionList.get(0).getDataSql();
	    	}
	    	dataSql = dataSql.replaceAll("#"+paramName+"#", "'"+ paramValue +"'");
	    	_logger.info("级联选择查询条件sql："+dataSql);
	    	dataList = jdbcTemplateDao.getConditionValues(dataSql);
	    }catch(Exception e){
			e.printStackTrace();
		}
    	return dataList;
    }
    
    @RequestMapping(value = "/isHaveCharts/{reportId}", method = RequestMethod.GET)
    @ResponseBody
    public String isHaveCharts(@PathVariable Long reportId){
    	//1  yes 0 no
    	Boolean isHaveCharts = columnConfService.isHaveCharts(reportId);
		if (isHaveCharts){
			return "1";
		}else {
			return "0";
		}
    }
    
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    @ResponseBody
    public void exportExcel(@RequestParam(value = "paramStr") String paramStr,HttpServletResponse response){
//    	@PathVariable String reportId {"reportId":"306","ITEM_NAME":"","ITEM_VAL":"","NAME":""}
    	
    	_logger.info("执行报表导出方法exportExcel");
    	List<Map<String, Object>> dataList = null;
    	List<ReportColumn> reportColList = null;
    	 List<ReportColumnGroup> reportColumnGroupList = null;
    	String fileName =getI18NMessage("common.report");//common.report
    	_logger.info("报表导出所传参数:"+paramStr);
    	//deal param string
    	Map<String, Object> paramMap = new HashMap<String, Object>();
    	String[] paramArray = paramStr.replace("{", "").replace("}", "").split(",");
    	for (String param : paramArray) {
    		String[] paramAndVal = param.split(":");
    		if (paramAndVal.length < 2){
    			continue;
    		}
    		paramMap.put(paramAndVal[0].replace("\"", ""), paramAndVal[1].replace("\"", ""));
		}
    	try{
    		//deal paramStr
    		String reportId = (String) paramMap.get("reportId");
    		reportId = reportId.replaceAll(",","");
        	_logger.info("reportI:"+reportId);
        	reportColList = reportColumnRepo.findByReportIdOrderBySortOrderAsc(Long.valueOf(reportId)); 
        	reportColumnGroupList = groupRepo.findByReportId(Long.valueOf(reportId));
        	_logger.info("reportColList:"+reportColList.size());
        	Report report = reportRepository.findOne(Long.valueOf(reportId));
        	fileName = report.getCode();
        	String dataSql = report.getSql();
        	if (dataSql != null && !dataSql.equals("")){
//        		Iterator itera = paramMap.entrySet().iterator();    	
//            	while(itera.hasNext()){
//            		//_logger.info("---------Map数据----------");
//            		Map.Entry entry = (Map.Entry) itera.next();
//            		String key = (String) entry.getKey();
//            		String value = (String) entry.getValue();
//            		_logger.info(key+":"+value);
//            		if (dataSql.indexOf("#"+key+"#") > -1 && !value.equals("") && value != null){
//            			dataSql = dataSql.replaceAll("#"+key+"#", "'"+value+"'");
//            		}
//            	
            	//dataSql = dataSql.replaceAll("where city_code =#cityName#", "");
        		dataSql = baseService.doSql(dataSql, paramMap);
            	dataList = jdbcTemplateDao.getConditionValues(dataSql);
        	}else{
        		_logger.error("报表查询sql为空，请详查！！");
        	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
    	poiService.download(response, fileName, reportColList, dataList,reportColumnGroupList);
    	
    }
    
    private List<Map<String, Object>> queryDataWithParam(String reportId,Map<String, Object> paramMap){
    	
    	List<Map<String, Object>> dataList = null;
    	try{
    		Report report = reportRepository.findOne(Long.valueOf(reportId));
	    	if (report.getSql() != null && !report.getSql().equals("")){
	    		dataList = jdbcTemplateDao.getConditionValues(report.getSql());
	    	}
	    }catch(Exception e){
			e.printStackTrace();
		}
    	return dataList;
    }
    
    private List<Map<String, Object>> queryData(String reportId){
    	List<Map<String, Object>> dataList = null;
    	try{
    		Report report = reportRepository.findOne(Long.valueOf(reportId));
        	if (report.getSql() != null && !report.getSql().equals("")){
        		dataList = jdbcTemplateDao.getConditionValues(report.getSql());
        	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
    	return dataList;
    }
    /**
     * 通过参数字段去查询报表执行sql
     * @param sqlList
     * @param mapParam
     * @return
     */
//    private String getQuerySql(List<ReportQuerySql> sqlList,Map<String, Object> mapParam){
//    	String defaultSql = "";
//    	String querySql ="";
//    	for (ReportQuerySql reportQuerySql : sqlList) {//判断查询语句的param是否在前台传过来的参数上面
//    		String queryParam = reportQuerySql.getQueryParam();
//    		if(queryParam != null && !queryParam.equals("")){
//    			String[] paramArray = queryParam.split(",");
//    			String flag = "0";
//    			for (int i = 0; i < paramArray.length; i++) {
//					Object paramObj = mapParam.get(paramArray[i]);
//					if(paramObj == null || paramObj.toString().equals("")){
//						flag = "1";
//						break;
//					}				    
//				}
//    			if(flag.equals("0")){
//    				querySql =  reportQuerySql.getQuerySql();
//    			}
//    		}else{
//    			defaultSql = reportQuerySql.getQuerySql();
//    		}
//		}
//    	if (querySql.equals("")){
//    		return defaultSql;
//    	}
//    	return querySql;
//    }
    /**
     * 通过级联关系去查询报表sql
     * @param sqlList
     * @param mapParam
     * @return
     */
//    private String getQuerySqlWithCascade(List<ReportQuerySql> sqlList,Map<String, Object> mapParam){
//    	String defaultSql = "";
//    	String querySql ="";
//    	List<ReportQuerySql> backupSqlList = new ArrayList<ReportQuerySql>();
//    	for (ReportQuerySql reportQuerySql : sqlList) {//判断查询语句的param是否在前台传过来的参数上面
//    		String queryParam = reportQuerySql.getQueryParam();
//    		if(queryParam != null && !queryParam.equals("")){
//    			String[] paramArray = queryParam.split(",");
//    			String flag = "0";
//    			for (int i = 0; i < paramArray.length; i++) {
//					Object paramObj = mapParam.get(paramArray[i]);
//					if(paramObj == null || paramObj.toString().equals("")){
//						flag = "1";
//						break;
//					}				    
//				}
//    			if(flag.equals("0")){
//    				backupSqlList.add(reportQuerySql);
//    				//querySql =  reportQuerySql.getQuerySql();
//    			}
//    		}else{
//    			defaultSql = reportQuerySql.getQuerySql();
//    		}
//		}
//    	if (backupSqlList.size() == 1){
//    		querySql =  backupSqlList.get(0).getQuerySql();
//    	}else if(backupSqlList.size() > 1){
//    		Long reportId = backupSqlList.get(0).getReport().getId();
//    		List<ReportCondition> conditionList = reportConditionRepo.findByReport_idAndCascadeConditionNotNull(reportId);
//    		//取参数个数最多值
//    		int maxParamNum = 0;
//    		int maxNum = 0;
//    		ReportQuerySql reportQueryUsed = null; 
//    		for (ReportQuerySql reportQuerySql : backupSqlList) {
//    			String[] paramCode = reportQuerySql.getQueryParam().split(",");
//    			if (paramCode.length >= maxParamNum+1){
//    				maxNum = 0;
//    				maxParamNum = paramCode.length;
//    				reportQueryUsed = reportQuerySql;
//    			}else if(paramCode.length == maxParamNum){
//    				maxNum = maxNum + 1;
//    			}
//    			
//    		}
//    		if (reportQueryUsed != null && maxNum == 0){
//    			querySql = reportQueryUsed.getQuerySql(); 
//    		}else {
//    			for (ReportQuerySql reportQuerySql : backupSqlList) {
//        			String[] paramCode = reportQuerySql.getQueryParam().split(",");
//        			for (int i = 0; i < paramCode.length; i++) {
//    					for (ReportCondition condition : conditionList) {
//    						if (condition.getCode().equals(paramCode[i])){
//    							querySql = reportQuerySql.getQuerySql();
//    						}
//    						
//    					}
//    				}
//    			}
//    		}		
//    	}
//    	if (querySql.equals("")){
//    		return defaultSql;
//    	}
//    	return querySql;
//    }
    
}
